import json
import re
import redis

from django.shortcuts import render
from .models import *
from rest_framework.views import APIView
from rest_framework.response import Response
from .ser import *
from django.core.paginator import Paginator


# Create your views here.


class NewsM(APIView):
    def post(self, request):
        ser = NewsSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200})
        else:
            print(ser.errors)
            return Response({"code": 10010})

    def get(self, request):
        gain = News.objects.all()
        p = request.GET.get('page')
        page = Paginator(gain, 2)
        result = page.page(p)
        ser = NewsSer(result, many=True)
        return Response({'code': 200, 'matter': ser.data, 'tpages': page.num_pages})


class GgM(APIView):
    def post(self, request):
        gain = Ggser(data=request.data)
        if gain.is_valid():
            gain.save()
            return Response({'code': 200})
        return Response({'code': 10010})

    def get(self, request):
        gain = Gg.objects.all()
        p = request.GET.get('p')
        page = Paginator(gain, 2)
        result = page.page(p)
        ser = Ggser(result, many=True)
        return Response({'code': 200, 'matter': ser.data, 'tpages':page.num_pages})




# class Text(APIView):
#     def get(self, request):
#         accId = '8aaf07087a331dc7017ae5ce36e83649'
#         accToken = '5ea139cb96174d499534494e9489904c'
#         appId = '8aaf07087a331dc7017ae5ce37be3650'
#         sdk = SmsSDK(accId, accToken, appId)
#         tid = '1'
#         datas = (236236, 1)
#         resp = sdk.sendMessage(tid, 13289792767, datas)
#         result = json.loads(resp)
#         if result['statusCode'] == '000000':
#             return 1
#         return 2
#
#
# class Register(APIView):
#     def post(self, request):
#         # 验证用户名是否为空
#         username = request.data.get('username')
#         if not username:
#             return Response({'code': 10010})
#         # 格式是否正确
#         name = re.match('\w{5,10}$', username)
#         if not name:
#             return Response({'code': 10011})
#         # 是否唯一
#         user = User.objects.first(username=username).first()
#         if user:
#             return Response({'code': 10012})
#
#         # 手机号
#         mobile = request.data.get('mobile')
#         if not mobile:
#             return Response({'code': 10013})
#         mobile = re.match('/^1[3-9]d{9}$/', mobile)
#         if not mobile:
#             return Response({'code': 10014})
#         # 是否唯一
#         mobile = User.objects.first(mobile=mobile).first()
#         if mobile:
#             return Response({'code': 10015})
#
#         return Response({'code': 200})
#
#
#
# from ronglian_sms_sdk import SmsSDK
# accId = '8a216da878005a800178a251439d39da'
# accToken = 'dc4fd2e2a23e45a68474dbc6d1233eda'
# appId = '8a216da878005a800178a251449839e1'
# import json
# def send_message(mobile,code,time):
#     sdk = SmsSDK(accId, accToken, appId)
#     tid = '1'
#     datas = (code, time)
#     resp = sdk.sendMessage(tid, mobile, datas)
#     result = json.loads(resp)
#     if result['statusCode'] == '000000':
#         return 1
#     return 2
#
#
# from utils.comm import send_message
# import random
# class SendSms(APIView):
#     def post(self,request):
#         mobile = request.data.get('mobile')
#         code = random.randint(100000,999999)
#         res = send_message(mobile,str(code),1)
#         code = 10010
#         if res:
#             code = 200
#         return Response({"code":code})
#
# import random, re
# from rest_framework.response import Response
# from rest_framework.views import APIView
# from utils.comm import send_message, get_redis
# from users.models import *


# 测试
class Text(APIView):
    def get(self, request):
        conn = get_redis()
        conn.setex('username', 10, 'xiaoming236')
        name = conn.get('username')
        return Response(name)


# 注册
class Register(APIView):
    def post(self, request):

        # 用户名
        username = request.data.get('username')
        if not username:
            return Response({'code': 10010})
        # 格式是否正确
        name = re.match('\w{5,10}$', username)
        if not name:
            return Response({'code': 10011})
        # 是否唯一
        user = User.objects.filter(username=username).first()
        if user:
            return Response({'code': 10012})

        # 密码
        password = request.data.get('password')
        if not password:
            return Response({'code': 10013})
        # 格式是否正确
        pwd = re.match('\w{3,6}$', password)
        if not pwd:
            return Response({'code': 10014})

        # 手机号
        mobile = request.data.get('mobile')
        if not mobile:
            return Response({'code': 10016})
        phone = re.match('^1[3-9]\d{9}$', mobile)
        if not phone:
            return Response({'code': 10017})
        # 是否唯一
        sloe = User.objects.filter(mobile=mobile).first()
        if sloe:
            return Response({'code': 10018})

        # 验证码验证
        smscode = request.data.get('smscode')
        conn = get_redis()
        code = conn.get(mobile)
        if code:
            code = str(code, encoding='utf8')
        if smscode != code:
            return Response({'code': 10019})

        return Response({'code': 200, 'meg': '注册成功'})


# 验证码
class SendSms(APIView):
    def post(self, request):
        mobile = request.data.get('mobile')
        codesms = random.randint(1000, 9999)
        res = send_message(mobile, str(codesms), 1)
        code = 10010
        if res:
            code = 200
            # 发生成功后存入redis
            conn = get_redis()
            conn.setex(mobile, 60, codesms)
        return Response({'code': code})




from django.http import HttpResponse
from utils.captcha.captcha import captcha
class CreateCaptcha(APIView):
    def get(self,request):
        name,text,img=captcha.generate_captcha()
        return HttpResponse(img,content_type='image/jpg')